package BookManage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class daoUser {
    public User findUserByName(String name){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try{
            connection=databaseOperation.getConnection();
            String sql="select * form user where name=?";
            statement=connection.prepareStatement(sql);
            statement.setString(1,name);
            resultSet=statement.executeQuery();
            if(resultSet.next()){
                boolean bool=(resultSet.getInt("isAdmin")==1);
                User user=null;
                if(bool){
                    user=new adminUser();
                }else{
                    user=new plainUser();
                }
                user.setUserId(resultSet.getInt("id"));
                user.setName(resultSet.getString("name"));
                user.setPassword(resultSet.getString("password"));
                return user;
            }else{
                return null;
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            databaseOperation.close(connection,statement,resultSet);
        }
        return null;
    }
}
